<?php

namespace Api\Sms\Yunpian;

use Api\Sms\SmsRepository;
use Yunpian\Sdk\YunpianClient;

class YunpianRepository extends SmsRepository
{
    /**
     * 发送短信验证码
     * 
     * @param  string $code        验证码
     * @param  string $countryCode 国际区号
     * @param  string $mobile      手机号码
     * @return array  [$smsStatus, $smsCallbackMessage]
     */
    protected function _sendVerificationCode(string $code, string $countryCode, string $mobile)
    {
        if ($countryCode === '86') {
            $content = str_replace('#code#', $code, $this->config['verificationCodeTemplate']['cn']);
        } else {
            $content = str_replace('#code#', $code, $this->config['verificationCodeTemplate']['en']);
        }
        $clnt  = YunpianClient::create($this->config['apiKey']);
        $param = [
            YunpianClient::MOBILE => '+'.$countryCode.$mobile,
            YunpianClient::TEXT   => $content,
        ];
        $r = $clnt->sms()->single_send($param);
        $smsStatus = $r->isSucc();
        $smsCallbackMessage = $r->msg().'：'.$r->detail();
        return [$smsStatus, $smsCallbackMessage];
    }

}
